<template>
  <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible="crud.status.cu > 0" :title="crud.status.title" width="700px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-form-item label="父级">
        <el-select v-model="form.parentId" placeholder="请选择">
          <el-option v-for="item in buildings" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="crud.cancelCU">取消</el-button>
      <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import CRUD, { form } from '@crud/crud'
import { getList } from '@/api/business/bed/building'

const defaultForm = { id: null, hospitalId: null, parentId: null, isNode: null, level: null, name: null, buildingId: null, buildingName: null, floorId: null, floorName: null }
export default {
  mixins: [form(defaultForm)],
  data() {
    return {
      hospitalId: null,
      buildings: [],
      rules: {
        name: [{ required: true, message: '建筑名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.hospitalId = this.$route.params.hospitalId
  },
  methods: {
    getBuildings() {
      getList({ hospitalId: this.hospitalId, parentId: 0 }).then((data) => {
        this.buildings = data
      })
    },
    [CRUD.HOOK.beforeToCU](crud, form) {
      this.getBuildings()
      form.hospitalId = this.hospitalId
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
